<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>人员管理</title>
<script src="js/jquery-1.9.1.min.js"></script>

<script src="js/jquery.mCustomScrollbar.concat.min.js"></script>
<link href="css/jquery.mCustomScrollbar.css" rel="stylesheet" type="text/css">

<script src="js/jquery.jtip.js"></script>
<link href="css/jquery.jtip.css" rel="stylesheet" type="text/css">

<script src="js/rb.js"></script>
<link href="css/rb.css" rel="stylesheet" type="text/css">

<script src="js/dump.js"></script>
<script>
$(document).ready(function() {
	$(".rb_mainframe_middle").eq(0).css("width",FRAMEWIDTH-230+"px");
	$(".rb_mainframe_middle").eq(1).css("width","200px");
	$(".rb_mainframe_middle").eq(1).css("right","10px");
	$(".rb_mainframe_middle_content").mCustomScrollbar({
    	theme:"dark",
		advanced:{updateOnContentResize:true}
	});
	//$(".rb_mainframe_middle_content").css("height",$(".rb_mainframe_middle").eq(0).height()-40+"px");
	$(".rb_mainframe_middle").eq(1).mCustomScrollbar({
    	theme:"dark",
		advanced:{updateOnContentResize:true}
	});
	
	
	getMemberList(1);
	getDepListNoTreeData("noTreeDepList");
	
	$(".rb_mainframe_middle p").click(function(){
		$(".rb_mainframe_middle p").each(function(){
			$(this).css("background-color","transparent");
			$(this).css("color","#000");
		});
		$(this).css("background-color","#36F");
		$(this).css("color","#fff");
		
	});
});

//获得非树状部门列表
var selectDepId = -1;
function getDepListNoTreeData(divID,depId){
	var str = "";
	$.ajax({
		timeout:2000, 
		type: AJAXMETHOD,
        async: false,
        url: SERVER+"dept/ajax/public",
        dataType: AJAXJSON,
        jsonp: "callback",
        jsonpCallback:"callback",
        success: function(json){
			//dump(json);
			for (var i=0;i<json.respList.length;i++){
				str += "<p id='dep_"+json.respList[i].id+"' val='"+json.respList[i].id+"'>"+json.respList[i].name+"</p>";				
			}
			
			$("#"+divID).html(str);
			
			$("#"+divID+" p").click(function(){
				$("#"+divID+" p").each(function(){
					$(this).css("background-color","transparent");
					$(this).css("color","#000");
				});
				$(this).css("background-color","#36F");
				$(this).css("color","#fff");
				if (divID == "noTreeDepList"){
					getMemberList($(this).attr("val"));
				}else{
					selectDepId = $(this).attr("val");
				}
			});
			$("#depList").mCustomScrollbar({
				theme:"dark",
				advanced:{updateOnContentResize:true}
			});
        },
		complete: function(){
			if (depId != undefined){			
				$("#depList #dep_"+depId).css("background-color","#36F");
				$("#depList #dep_"+depId).css("color","#fff");
			}				
		},
        error: function(){
            
    	}
	});
}

//获得角色列表
var roleArray = new Array(); 
function getRoleList(roleArr,depId){
	//将传递过来的String转成数组
	if (roleArr != undefined){
		var roleObj = roleArr.split(",");
	}else{
		var roleObj = [];
	}
	var str = "";
	$.ajax({
		timeout:2000, 
		type: AJAXMETHOD,
        async: false,
        url: SERVER+"role",
        dataType: AJAXJSON,
        jsonp: "callback",
        jsonpCallback:"callback",
        success: function(json){
			//dump(json);
			//获取部门非树状
			getDepListNoTreeData("depList",depId);
			for (var i=0;i<json.respBody.data.length;i++){
				str += "<p style='background-image:url(assets/widget_checkbox_icon.png);background-repeat:no-repeat;background-position:right 10px center;background-size:25px;' id='role_"+json.respBody.data[i].id+"' val='"+json.respBody.data[i].id+"'>"+json.respBody.data[i].name+"</p>";				
			}
			$("#roleList").html(str);
			$("#roleList p").click(function(){				
				if ($(this).css("background-image").indexOf("widget_checkbox_icon.png")>0){					
					$(this).css("background-image","url(assets/widget_checkbox_icon_selected.png)");
					$(this).css("color","#FF4704");
					//$(this).css("font-weight","bold");
					roleArray.push($(this).attr("val")+",");
				}else{
					$(this).css("background-image","url(assets/widget_checkbox_icon.png)");
					$(this).css("color","#000");
					$(this).css("font-weight","normal");
					for (var i=0;i<roleArray.length;i++) {
						if (roleArray[i] === $(this).attr("val")+",") {
							roleArray.splice(i, 1);
						}
					}
					//roleArray.remove($(this).attr("val"));
				}
				
			});
			$("#roleList").mCustomScrollbar({
				theme:"dark",
				advanced:{updateOnContentResize:true}
			});
        },
		complete: function(){			
			for (var j=0;j<roleObj.length;j++){
				$("#role_"+roleObj[j]).css("background-image","url(assets/widget_checkbox_icon_selected.png)");
				$("#role_"+roleObj[j]).css("color","#FF4704");
				//$("#role_"+roleObj[j]).css("font-weight","bold");
			}
		},
        error: function(){
            
    	}
	});	
}

//获取人员列表数据
function getMemberList(depid){
	$("#report tr:gt(0)").children().remove();
	$.ajax({
		timeout:2000, 
		type: AJAXMETHOD,
        async: false,
        url: SERVER+"user?deptId="+depid,
        dataType: AJAXJSON,
        jsonp: "callback",
        jsonpCallback:"callback",
        success: function(json){
			for (var i=0;i<json.respBody.data.length;i++){
				var roleArr = new Array();
				var roleStr = "";
				for (var j=0;j<json.respBody.data[i].roles.length;j++){
					roleArr.push(json.respBody.data[i].roles[j].id);
					roleStr += json.respBody.data[i].roles[j].name+",";
				}
				if (roleArr.length>0){
					$("#report").append("<tr id='"+json.respBody.data[i].id+"'><td><img class='report_tx' src='assets/temp_tx.jpg' /></td><td>"+json.respBody.data[i].realName+"</td><td>"+json.respBody.data[i].userName+"<input type='hidden' value='"+json.respBody.data[i].email+"'/></td><td>"+json.respBody.data[i].deptName+"</td><td>"+roleStr+"</td><td><a href=\"javascript:JT_editUser('item_"+json.respBody.data[i].id+"','修改人员信息','"+roleArr+"','"+json.respBody.data[i].deptId+"')\" id='item_"+json.respBody.data[i].id+"'>修改</a> <a href='javascript:RB.AlertInfo(\"禁止操作\",\"该用户已经分配了角色，严禁删除，请先移除用户角色后再进行删除操作！\");'>删除</a></td></tr>");
				}else{
					$("#report").append("<tr id='"+json.respBody.data[i].id+"'><td><img class='report_tx' src='assets/temp_tx.jpg' /></td><td>"+json.respBody.data[i].realName+"</td><td>"+json.respBody.data[i].userName+"<input type='hidden' value='"+json.respBody.data[i].email+"'/></td><td>"+json.respBody.data[i].deptName+"</td><td>"+roleStr+"</td><td><a href=\"javascript:JT_editUser('item_"+json.respBody.data[i].id+"','修改人员信息','"+roleArr+"','"+json.respBody.data[i].deptId+"')\" id='item_"+json.respBody.data[i].id+"'>修改</a> <a href='javascript:alertUserDel(\""+json.respBody.data[i].id+"\",\""+json.respBody.data[i].realName+"\",\""+json.respBody.data[i].deptId+"\");'>删除</a></td></tr>");
				}
			}
			if (json.respBody.data.length==0){
				$("#report").append("<tr><td colspan='6' align='center'>该部门下暂无人员</td></tr>");
			}
			//底部占位
			$("#report").append("<tr><td height='150px;'></td></tr>");
        },
		complete:function(){
			$("#dep_"+depid).css("background-color","#36F");
			$("#dep_"+depid).css("color","#fff");
        },
        error: function(){
            
    	}
	});	
	$("#noTreeDepList p").each(function(){
		$(this).css("background-color","transparent");
		$(this).css("color","#000");
	});
}

//删除人员确认询问
function alertUserDel(id,username,depId){
	RB.Alert("删除用户确认","您确认要删除<b>\""+username+"\"</b>用户吗？", "userDel('"+id+"','"+username+"','"+depId+"');");
}
//删除人员
function userDel(id,username,depId){
	$.ajax({
		timeout:2000, 
		type: AJAXMETHOD,
        async: false,
        url: SERVER+"user/delete/"+id,
        dataType: AJAXJSON,
        jsonp: "callback",
        jsonpCallback:"callback",
        success: function(json){
			RB.Toast("删除<b>\""+username+"\"</b>用户成功！");
			getMemberList(depId);
        },
        error: function(){
            
    	}
	});	
}

//创建人员
function createUser(){
	var depId = selectDepId;
	var username = $("#createUserName").val();
	var realname = $("#createRealName").val();
	var email = $("#createEmail").val();
	var roleIdArr = roleArray.join("");
	roleIdArr = roleIdArr.substring(0,roleIdArr.length-1);
	if (depId<0){
		RB.Toast("部门不能为空！");
	}else if (username.length<=0){
		RB.Toast("用户名不能为空！");
	}else if(realname.length<=0){
		RB.Toast("真实姓名不能为空！");
	}else{
		$.ajax({
			timeout:2000, 
			type: AJAXMETHOD,
			async: false,
			url: SERVER+"user/add/user?isValid=1&userType=1&userName="+username+"&realName="+realname+"&email="+email+"&deptId="+depId+"&roleIds="+roleIdArr,
			dataType: AJAXJSON,
			jsonp: "callback",
			jsonpCallback:"callback",
			success: function(json){
				//dump(json);	
				jt_close();
				RB.Toast("创建<b>\""+realname+"\"</b>用户成功！");	
				getMemberList(depId);
				var y = $("#dep_"+depId).offset().top-300;
				$(".rb_mainframe_middle").eq(1).mCustomScrollbar("scrollTo",y);
				roleArray = [];
				
			},
			error: function(){
				alert("CreateUser Error!");
			}
		});
	}
}
//修改人员
function editUser(userId){
	var depId = "";
	$("#depList p").each(function(index, element) {
        if ($(this).css("color").indexOf("255")>0){
			depId = $(this).attr("val");
		}
    });
	var username = $("#editUserName").val();
	var realname = $("#editRealName").val();
	var email = $("#editEmail").val();
	var roleArr = "";
	$("#roleList p").each(function(index, element) {
        if ($(this).css("background-image").indexOf("widget_checkbox_icon_selected.png")>0){
			roleArr += $(this).attr("val")+",";
		}
    });
	roleArr = roleArr.substring(0,roleArr.length-1);
	if (username.length<=0){
		RB.Toast("用户名不能为空！");
	}else if(realname.length<=0){
		RB.Toast("真实姓名不能为空！");
	}else{
		$.ajax({
			timeout:2000, 
			type: AJAXMETHOD,
			async: false,
			url: SERVER+"user/add/user?id="+userId+"&isValid=1&userType=1&userName="+username+"&realName="+realname+"&email="+email+"&deptId="+depId+"&roleIds="+roleArr,
			dataType: AJAXJSON,
			jsonp: "callback",
			jsonpCallback:"callback",
			success: function(json){
				//dump(json);	
				jt_close();
				RB.Toast("修改<b>\""+realname+"\"</b>用户成功！");	
				getMemberList(depId);
				var y = $("#dep_"+depId).offset().top-300;
				$(".rb_mainframe_middle").eq(1).mCustomScrollbar("scrollTo",y);
				roleArray = [];
				
			},
			error: function(){
				alert("EditUser Error!");
			}
		});
	}
}

</script>
<style type="text/css">
a{
	color 				: #ff0000;	
	text-decoration		: none;
}

a:hover{
	text-decoration		: underline;
}
#report { border-collapse:collapse;}
#report h4 { margin:0px; padding:0px;}
#report ul { margin:10px 0 10px 10px; padding:0px;}
#report ul li {
	float:left;
	width:30%;
	height:40px;
	list-style-type:none;
	padding:10px;
	font-size:15px;
}
#report ul li div {
	float:left;
	height:40px;
	line-height:40px;
	margin-left:10px;
	
}
#report ul li div:nth-child(2){
	font-weight:bold;
	width:50%;
}
#report table{ width:100%;}
#report th { background:#6e8187; color:#fff; padding:7px 15px; text-align:left;width:22%;height:30px;}
#report th:first-child {width:50px;}
#report td { background:#fff none repeat-x scroll center left; color:#000; padding:20px 10px;color:#5a3c44; border-bottom:#f0f0f0 1px solid; }
#report tr.odd td { background:#fff; cursor:pointer;color:#837076; }
#report tr.odd td.up { background:#fbe7ce; cursor:pointer; font-weight:bold; border-top:#FFF 1px solid; color:#5a3c44; }
#report tr.odd td.color { color:#fbe7ce;}
#report div.arrow { background:transparent url(assets/arrows.png) no-repeat scroll 0px -16px; width:16px; height:16px; display:block;}
#report div.up { background-position:0px 0px;}
.report_tx{
	width:40px;
	height:40px;
	background-size:40px 40px;
	float:left;
	border-radius:20px;
	box-shadow: 3px 3px 3px #888888;
}
.rb_mainframe_middle_title{
	background-color:#6e8187;
}
</style>
</head>

<body onselectstart="return false"> 
<div class="rb_mainframe_top">
	<div class="rb_mainframe_top_title_first">
    	组织结构
    </div>
    <div class="rb_mainframe_top_title_second">
    	人员管理
    </div>    
    <div class="rb_mainframe_top_btn">帮助</div>   
  	<div class="rb_mainframe_top_btn"><a href="javascript:JT_createUser('createUserShow','新建人员');" id="createUserShow">新建人员</a></div>    
    <div class="rb_mainframe_top_input"><img src="assets/top_input_icon_search.png"/><input type="text" placeholder="输入人员姓名..." name="searchDep" id="searchDep" /><input name="sumbit" type="button" value="搜索" onClick="searchDep(this.value)"></div>

</div>

<div class="rb_mainframe_content">

    <div class="rb_mainframe_middle">
        <div class="rb_mainframe_middle_content">
            <table id="report">
                <tr>
                    <th></th>
                    <th>真实姓名</th>
                    <th>用户名</th>
                    <th>所属部门</th>
                    <th>角色</th>
                    <th>操作</th>
                </tr>
                
            </table>
        </div>
        <!--
        <div class="rb_mainframe_middle_page">
        	<div class="rb_mainframe_middle_bottom_btn_left">首页</div>
            <div class="rb_mainframe_middle_bottom_btn_left">上一页</div>
            <div class="rb_mainframe_middle_bottom_btn_left">下一页</div>
            <div class="rb_mainframe_middle_bottom_btn_left">尾页</div>
        </div>
        -->
    </div>
    <div class="rb_mainframe_middle">
        <div class="rb_mainframe_middle_title">部门列表</div>
        <div id="noTreeDepList">
			<p>加载中...</p>
        </div>
    </div>
</div>
</body>
</html>
